package queens.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.Resources;

import queens.beans.ClienteBean;
import queens.facades.IClienteFacade;
import queens.forms.BusquedaClientesForm;

public class BusquedaClientesAction extends QueensAction {
	
	private IClienteFacade clienteFacade;
	
	private void inicializarFacades(){
		clienteFacade = (IClienteFacade)getBean("clienteFacade");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		ActionErrors errors                        = (ActionErrors)request.getSession().getAttribute(Globals.ERROR_KEY);
		boolean errores                  		   = errors.size()>0;
		BusquedaClientesForm busquedaClientesForm  = (BusquedaClientesForm)form;
		String forward                             = "busquedaClientes";
		inicializarFacades();
		
		if(!errores){
			if((Resources.getMessage(request,"buscar").equals(busquedaClientesForm.getAccion())) || ("buscar".equals(busquedaClientesForm.getAccion()))){
				List listaClientes = getClienteFacade().buscarClientes(busquedaClientesForm.getBusquedaClientesBean());
				busquedaClientesForm.setListaClientes(listaClientes);
				request.setAttribute("busquedaRealizada", "busquedaRealizada");
			}else if(Resources.getMessage(request,"editar").equals(busquedaClientesForm.getAccion())){
				forward = "editarClienteAction";
			}else if(Resources.getMessage(request,"borrar").equals(busquedaClientesForm.getAccion())){
					forward = "borrarClienteAction";
			}else if(Resources.getMessage(request,"seleccionar").equals(busquedaClientesForm.getAccion())){
				ClienteBean clienteBean = getClienteFacade().obtenerCliente(busquedaClientesForm.getBusquedaClientesBean().getDni());
				if(clienteBean != null){
					List listaClientes = new ArrayList<ClienteBean>();
					listaClientes.add(clienteBean);
					busquedaClientesForm.setListaClientes(listaClientes);
				}
			}else if(Resources.getMessage(request,"listado").equals(busquedaClientesForm.getAccion())){
				List listaClientes = getClienteFacade().listadoClientes(busquedaClientesForm.getBusquedaClientesBean().getOrdenacionListado());
				busquedaClientesForm.setListaClientes(listaClientes);
			}else if(Resources.getMessage(request,"salir").equals(busquedaClientesForm.getAccion())){
				forward = "inicio";
			}
		}else{// hay errores
			if((Resources.getMessage(request,"editar").equals(busquedaClientesForm.getAccion())) || (Resources.getMessage(request,"borrar").equals(busquedaClientesForm.getAccion()))){
				List listaClientes = getClienteFacade().buscarClientes(busquedaClientesForm.getBusquedaClientesBean());
				busquedaClientesForm.setListaClientes(listaClientes);
				request.setAttribute("busquedaRealizada", "busquedaRealizada");
			}
		}
		return mapping.findForward(forward);
	}
	
	protected IClienteFacade getClienteFacade() {
		return clienteFacade;
	}
}